package com.infoguia;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.infoguia.util.Constants;
import com.infoguia.db.*;
import com.infoguia.to.*;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class SearchResults extends ListActivity {

	private static final int ACTIVITY_CATEGORY_DETAIL = 1;
	private InfoguiaDao dao;
	public static String STR_SEARCH;;
	public static Long STATE_SEARCH;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		String strSearchCategory;
		Long stateSearch;

		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_results);

		strSearchCategory = getIntent().getExtras() != null ? getIntent()
				.getExtras().getString(Constants.STR_SEARCH) : null;
		stateSearch = getIntent().getExtras() != null ? getIntent().getExtras()
				.getLong(Constants.STATE_SEARCH) : null;

		this.loadCategories(strSearchCategory, stateSearch);
		this.loadCompanies(strSearchCategory, stateSearch);
		this.goHome();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent i = new Intent(this, DetailItem.class);
		TextView textView = (TextView) v.findViewById(R.id.idElement);	
		DetailItem.ID_COMPANY = textView.getText().toString().trim();
		i.putExtra(Constants.ID, id);
		startActivityForResult(i, ACTIVITY_CATEGORY_DETAIL);
	}

	private void loadCategories(String strSearch, Long stateSearch) {

		List<CategoryTo> search;
		String[] results;
		int i = 0;

		if (dao == null) {
			dao = new InfoguiaDao(this);
			dao.open();
		}

		if (stateSearch == 0) {
			search = dao.getCategoryByName(strSearch);
		} else {
			search = dao.getCategoryByState(strSearch, stateSearch);
		}

		results = new String[search.size()];
		for (CategoryTo cat : search) {
			results[i++] = ((cat.getName().length()>50 ? cat.getName().substring(0,49) + " ..." : cat.getName()) ) + " (" + cat.getCompany().size()
					+ ")";
		}

		ListView listSR = (ListView) findViewById(R.id.listSR);
		listSR.setAdapter(new ArrayAdapter<String>(this, R.layout.cr_row,
				R.id.textCR, results));

		listSR.setEmptyView(findViewById(R.id.emptyView));

		STR_SEARCH = strSearch;
		STATE_SEARCH = stateSearch;
		
		listSR.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long arg) {
				Intent i = new Intent(SearchResults.this, ResultList.class);
				i.putExtra(Constants.ID, arg);
				startActivityForResult(i, ACTIVITY_CATEGORY_DETAIL);
			}
		});
	}

	private void loadCompanies(String strSearch, Long stateSearch) {

		List<CompanyTo> companies;
		String[] results;
		int i = 0;

		if (dao == null) {
			dao = new InfoguiaDao(this);
			dao.open();
		}

		if (stateSearch == 0) {
			companies = dao.getCompanyByName(strSearch);
		} else {
			companies = dao.getCompanyByState(strSearch, stateSearch);
		}

		String[] from = new String[] { "row_0", "row_1"};
		int[] to = new int[] { R.id.textCR, R.id.idElement};
		
		List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
		
		for (CompanyTo cat : companies) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("row_0", cat.getName().length() > 50 ? cat.getName().substring(0,49) + " ...": cat.getName());
			map.put("row_1", String.valueOf(cat.getId()));		
			fillMaps.add(map);
		}
	
		setListAdapter(new SimpleAdapter(this, fillMaps, R.layout.cr_row, from,
				to));

	}
	
	public void goHome() {

		ImageView home = (ImageView) findViewById(R.id.home);

		home.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(SearchResults.this, StartActivity.class);
				startActivityForResult(i, 1);
			}
		});

	}

}
